﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Net.NetworkInformation;

namespace SalaryChannel
{
    public class OOBInstall
    {

        public bool IsInstalled()
        {
            if (Application.Current.InstallState == InstallState.Installed)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 程序安装
        /// </summary>
        public void Install()
        {
            if (!Application.Current.IsRunningOutOfBrowser)
            {
                if (Application.Current.InstallState != InstallState.Installed)
                {
                    Application.Current.Install();
                }
                else
                {
                    App.MessageBox.ShowDialog("程序已安装！");//, "系统提示", MessageBoxButton.OK);
                }

            }
            //调用InstallStateChanged事件将安装状态传递给调用者
            Application.Current.InstallStateChanged += new EventHandler(Current_InstallStateChanged);
        }

        void Current_InstallStateChanged(object sender, EventArgs e)
        {
            //此处调用自定义事件
            OnInstalling(sender, new InstallStateArgs());
        }

        //自定义一个事件来修改安装时状态
        public delegate void InstallingHandler(object sender, InstallStateArgs e);
        public event InstallingHandler Installing;
        public void OnInstalling(object sender, InstallStateArgs e)
        {
            if (Installing != null)
            {
                Installing(sender, e);
            }
        }

        /// <summary>
        /// 程序更新（如果需要更细你可以通过修改程序Assembly Version）
        /// </summary>
        public void Update()
        {
            //网络改变时事件
            NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);
            //判断程序是否在oob下运行并且网络可用
            if (Application.Current.IsRunningOutOfBrowser && NetworkInterface.GetIsNetworkAvailable())
            {
                Application.Current.CheckAndDownloadUpdateCompleted += new CheckAndDownloadUpdateCompletedEventHandler(Current_CheckAndDownloadUpdateCompleted);
                Application.Current.CheckAndDownloadUpdateAsync();
            }
        }

        void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
        {
            Console.WriteLine("网络发生改变，当前网络状态：{0}", NetworkInterface.GetIsNetworkAvailable().ToString());
        }

        void Current_CheckAndDownloadUpdateCompleted(object sender, CheckAndDownloadUpdateCompletedEventArgs e)
        {
            if (e.UpdateAvailable)
            {
                App.MessageBox.ShowDialog("程序已更新到最新版本，请重新启动！",new Action(Do_Reboot),Title.Information);//, "系统提示", MessageBoxButton.OK);
            }
            else
            {
                if (e.Error != null)
                {
                    App.MessageBox.ShowDialog("程序更新失败，错误信息如下：" + Environment.NewLine + e.Error.Message);//, "系统提示", MessageBoxButton.OK);
                }
            }
        }
        void Do_Reboot()
        {
            //AppDomain.CurrentDomain.clo
            try
            {
                App.Current.MainWindow.Close();
            }
            catch (Exception ex)
            {

                App.MessageBox.ShowDialog("关闭程序出错，请手动重启:" + ex.Message);
            }


        }

    }

    public class InstallStateArgs : EventArgs
    {
        public InstallState State
        {
            get
            {
                return Application.Current.InstallState;
            }
        }
    }

}
